package com.example.stlink.activitys;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.example.stlink.R;
import com.example.stlink.fragments.StuClassListFragment;
import com.example.stlink.fragments.StuHomeFragment;
import com.example.stlink.fragments.TeaCourseListFragment;
import com.example.stlink.fragments.TeaHomeFragment;
import com.example.stlink.fragments.UserInfoFragment;
import com.example.stlink.model.constants.ModelFieldConstants;
import com.example.stlink.utils.activityUtil.MainActivityUtil;
import com.google.android.material.badge.BadgeDrawable;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private MainActivityUtil mainActivityUtil;
    private BottomNavigationView bottomNavigationView;
    private Integer roleId;
    private Bundle bundle;

    private BadgeDrawable badge;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initPager();
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @SuppressLint("RestrictedApi")
    private void initView(){

        bottomNavigationView = findViewById(R.id.bottom_navigation_view);

        mainActivityUtil = new MainActivityUtil(this);
        mainActivityUtil.initView();

        bundle = getIntent().getExtras();
        roleId = (Integer) bundle.get(ModelFieldConstants.ROLE_ID);

        if(roleId == 0){
            bottomNavigationView.getMenu().getItem(2).setIcon(R.mipmap.student);
            bottomNavigationView.getMenu().getItem(1).setTitle("班级消息");
        }

        badge = bottomNavigationView.getOrCreateBadge(1);
//        orCreateBadge.setVisible(false);
        badge.setNumber(0);

    }

    /**
     * 绑定页面
     *
     * flutter
     * uniapp
     * kotlin
     */
    private void initPager(){
        List<Fragment> list = new ArrayList<>();
        if(roleId == 1){
            TeaHomeFragment teaHomeFragment = TeaHomeFragment.newInstance("主页",this,findViewById(R.id.nav_host_fragment));
            TeaCourseListFragment teaCourseListFragment = TeaCourseListFragment.newInstance("班级消息", this);
            list.add(teaHomeFragment);
            list.add(teaCourseListFragment);
        }else{
            StuHomeFragment stuHomeFragment = StuHomeFragment.newInstance("主页");
            StuClassListFragment stuClassListFragment = StuClassListFragment.newInstance("班级消息", MainActivity.this, badge);
            list.add(stuHomeFragment);
            list.add(stuClassListFragment);
        }
        UserInfoFragment teaInfoFragment = UserInfoFragment.newInstance("个人信息", MainActivity.this);
        list.add(teaInfoFragment);
        mainActivityUtil.initPager(list);
    }

    /**
     * 1、当按下BACK键时，会被onKeyDown捕获，判断是BACK键，则执行exit方法。
     * @param keyCode 自定义标识
     * @param event 用户事件
     * @return 用户是否点击系统返回键
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            mainActivityUtil.setExit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}
